package com.ibiaoke.boss.controller;

import com.ibiaoke.common.response.Res;
import com.ibiaoke.common.utils.LocationUtils;
import com.ibiaoke.system.dao.ShopDao;
import com.ibiaoke.system.entity.Shop;
import com.ibiaoke.system.entity.dto.DeliverInfoDTO;
import com.ibiaoke.system.entity.vo.XcxShopDetail;
import com.ibiaoke.system.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by biaoke on 2017/6/23.
 */
@Controller
public class WxShopController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private ShopDao shopDao;


    /**
     * 获取门店列表
     *
     * @return
     */
    @RequestMapping("/listShop")
    @ResponseBody
    public Res listShop(){
        List<Shop> shops = shopService.listShop();
        return new Res().addResponse("shops",shops);
    }

    /**
     * 获取配送费
     * @param longitude 经度
     * @param latitude 维度
     * @param productPrice 商品金额
     * @return
     */
    @RequestMapping("/getDeliveryInfo")
    @ResponseBody
    public Res getDeliveryInfo(Double longitude, Double latitude, BigDecimal productPrice){
        DeliverInfoDTO deliveryInfo = shopService.getDeliveryInfo(longitude, latitude, productPrice);
        return Res.create().addResponse("deliveryInfo",deliveryInfo);
    }

    /**
     * 获取附近门店信息
     * @param longitude
     * @param latitude
     * @return
     */
    @RequestMapping("/getNearestShop")
    @ResponseBody
    public Res getNearestShop(Double longitude, Double latitude) {
        /*Shop nearestShop = shopService.getNearestShop(longitude, latitude);
        return new Res().addResponse("nearestShop", nearestShop);*/

        //测试   2018-04-27
        Shop shop = shopDao.selectByPrimaryKey(1011);
        XcxShopDetail shopDetail =new XcxShopDetail();
        shopDetail.setId(shop.getShopId());
        shopDetail.setName(shop.getShopName());
        shopDetail.setBusinessTime(shop.getOpenBeginTime()+" - "+shop.getOpenEndTime().toString());
        Double distince = LocationUtils.getDistance(longitude, latitude, shop.getDesLongitude(), shop.getDesLatitude());
        DecimalFormat df = new DecimalFormat("######0.0");
        shopDetail.setDistince(Double.valueOf(df.format(distince/1000)));
        shopDetail.setShopowner(shop.getContactPerson());
        shopDetail.setMobile(shop.getShopMobile());
        shopDetail.setShopAddress(shop.getShopAddress());
        shopDetail.setDesLatitude(shop.getDesLatitude());
        shopDetail.setDesLongitude(shop.getDesLongitude());
        shopDetail.setStatus(shopDetail.getDistince() >= 15 ? 0 : 1);
        return new Res().addResponse("nearestShop", shopDetail);
    }
}
